<template>
  <uni-drawer ref="deptDrawer" mode="right" :mask-click="true" :width="330">
    <radio-dept-tree
      :checkList="checkList"
      v-if="tree.length > 0"
      keyValue="id"
      :showBar="showBar"
      :props="prop"
      @sendValue="deptClick"
      :isCheck="true"
      :treeNone="tree"
    ></radio-dept-tree>
  </uni-drawer>
</template>

<script>
import RadioDeptTree from './radio-dept-tree/tree.vue'
import { listDeptChildren, getDept } from '@/api/system/dept.js'
export default {
  name: 'DeptSelector',
  components: {
    RadioDeptTree
  },
  data() {
    return {
      searchKey: '',
      listData: [],

      tree: [], // 树形数据
      checkList: [], // 选中数据
      prop: {
        label: 'label', // 树形展示字段label
        children: 'children' // 树形子级字段children
      }
    }
  },
  props: {
    id: {
      type: [Number, Array, String]
    },
    // 页面使用自定义导航栏 navigationStyle: custom，需要设置参数为true
    showBar: {
      type: Boolean,
      default: () => false
    }
  },
  watch: {
    id: {
      async handler(newVal, oldVal) {
        if (newVal != oldVal && newVal) {
          const res = await getDept(newVal)
          const data = {
            ...res.data,
            name: res.data.deptName,
            value: res.data.deptId
          }
          this.$emit('change', data)
        }
      },
      deep: true,
      immediate: true
    }
  },
  onReady() {},
  methods: {
    open() {
      this.tree = []
      this.$refs.deptDrawer.open()
      this.getDept()
    },
    deptClick(item) {
      const data = {
        ...item,
        name: item.label,
        value: item.id
      }
      this.$emit('change', data)
      this.$refs.deptDrawer.close()
    },
    getDept() {
      // 缓存部门数据（60s）
      let deptTree = this.$simpleCache.get('deptTree')
      if (deptTree) {
        this.tree = this.arrToTree(deptTree)
      } else {
        listDeptChildren().then((res) => {
          this.tree = res.data
          this.$simpleCache.put('deptTree', res.data, 60)
        })
      }
    },
    // 转换成树形结构
    arrToTree(arr, parentId = 0) {
      const res = []
      arr.forEach((item) => {
        item.user = false
        if (item.parentId === parentId) {
          const children = this.arrToTree(
            arr.filter((v) => v.parentId !== parentId),
            item.id
          )
          if (children.length) {
            res.push({ ...item, children })
          } else {
            res.push({ ...item })
          }
        }
      })
      return res
    }
  }
}
</script>
<style lang="scss" scoped>
.example-body {
  padding: 10px;
}

.scroll-view {
  // padding-top: var(--status-bar-height);
  /* #ifndef APP-NVUE */
  width: 100%;
  height: 100%;
  /* #endif */
  flex: 1;

  ::v-deep .uni-searchbar__box {
    justify-content: flex-start;
  }

  .select-title {
    font-size: 32rpx;
    font-weight: 500;
    color: #666666;
    line-height: 45rpx;
    padding: 20rpx 30rpx 30rpx;
  }

  .select-all-wrap {
    display: flex;
    font-size: 32rpx;
    font-weight: 500;
    color: #333333;
    padding: 30rpx;
  }

  .select-icon {
    width: 40rpx;
    height: 40rpx;
    margin-right: 30rpx;
  }
}

// 处理抽屉内容滚动
.scroll-view-box {
  flex: 1;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: calc(100vh - 200rpx);
}

.info {
  padding: 15px;
  color: #666;
}

.info-text {
  font-size: 14px;
  color: #666;
}

.info-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24rpx 20rpx 24rpx 30rpx;

  .dept-icon-wrap {
    background: #f4f5f9;
    border-radius: 50%;
    width: 80rpx;
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 30rpx;

    .dept-icon {
      width: 44rpx;
      height: 36rpx;
    }
  }

  .dept-name {
    font-size: 32rpx;
    font-weight: 500;
    color: #333333;
  }

  .user-num {
    font-size: 32rpx;
    font-weight: 500;
    color: #999999;
  }
}

.close {
  padding: 10px;
}
</style>
